package com.bizmerce.rnd.oms.reporting.web;

import java.awt.Color;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.view.document.AbstractPdfView;

import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
/**
 * TODO 8.원산지기준 충족여부 박스 미흡
 * @author hanmi8
 *
 */
public class Form13 extends AbstractPdfView {

	@Override
	protected void buildPdfDocument(Map map, Document document, PdfWriter writer,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
	    
		/**FONTS*/
		BaseFont baseFont = BaseFont.createFont("HYGoThic-Medium", "UniKS-UCS2-H", BaseFont.NOT_EMBEDDED);
		Font headerFont = new Font(baseFont, 14, Font.BOLD, Color.black);
		Font normalFont = new Font(baseFont, 8, Font.NORMAL);
		Font boldFont = new Font(baseFont, 12, Font.NORMAL);
		Font formatlFont = new Font(baseFont, 9, Font.NORMAL);
		
		/**TITLE*/
		Chunk chunk;
		Paragraph paragraph = new Paragraph("<양식 13>수출용원재료 원산지확인서",headerFont);
		document.add(paragraph);
		
		chunk = new Chunk("[별지 제2호서식]", formatlFont);
		float subscript = -15.0f;
		chunk.setTextRise(subscript);
		document.add(chunk);

		/**TABEL INFORMATION*/
		Table table;
		Cell cell;
		table = new Table(11);
		table.setWidth(100);
		int headerwidths[] = {6, 4, 6, 6, 6, 12, 12, 6, 6, 8, 28};
		table.setWidths(headerwidths);
		table.setPadding(2);
		table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);

		cell = new Cell(new Paragraph("수  출  용  원  재  료    원  산  지 (포  괄)   확  인  서", headerFont));
		chunk = new Chunk("\n");
		cell.add(chunk);
		PdfContentByte cb = writer.getDirectContent();
        cb.beginText();
        cb.setFontAndSize(baseFont, 8);
        cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, "발급번호 : ", 470, 700, 0);
        cb.setFontAndSize(baseFont, 8);
        cb.showText("DFSFS2343");
        cb.endText();
		cell.setColspan(11);
		table.addCell(cell);
		
		/**1. 공급자*/
		cell = new Cell(new Paragraph("1.공급자",normalFont));
		cell.setRowspan(3);
		cell.setColspan(2);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		table.addCell(cell);
		cell = new Cell(new Paragraph("법인명(상호)",normalFont));
		cell.setColspan(2);
		table.addCell(cell);
		cell = new Cell(new Paragraph("비즈머스",normalFont));
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		cell.setColspan(4);
		table.addCell(cell);
		cell = new Cell(new Paragraph("사업자등록번호",normalFont));
		cell.setColspan(2);
		table.addCell(cell);
		cell = new Cell(new Paragraph("제3423-342-342",normalFont));
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		table.addCell(cell);
		
		cell = new Cell(new Paragraph("대표자(성명)",normalFont));
		cell.setColspan(2);
		table.addCell(cell);
		cell = new Cell(new Paragraph("대표자",normalFont));
		cell.setColspan(4);
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		table.addCell(cell);
		cell = new Cell(new Paragraph("전  화 / 팩  스",normalFont));
		cell.setColspan(2);
		table.addCell(cell);
		cell = new Cell(new Chunk("042-123-1234",normalFont));
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		chunk = new Chunk("/",normalFont);
		cell.add(chunk);
		chunk = new Chunk("042-567-9876",normalFont);
		cell.add(chunk);
		table.addCell(cell);
		
		cell = new Cell(new Paragraph("주소(전자주소)",normalFont));
		cell.setColspan(2);
		table.addCell(cell);
		cell = new Cell(new Paragraph("대전광역시 서구 만년동대전광역시 서구 만년동대전광역시 서구 만년동 ",normalFont));
		cell.setColspan(7);
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		table.addCell(cell);
		
		/**2. 공급처*/
		cell = new Cell(new Paragraph("2.공급처"+"\n"+"(수출자"+"\n"+"/생산자)",normalFont));
		cell.setRowspan(3);
		cell.setColspan(2);
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		table.addCell(cell);
		cell = new Cell(new Paragraph("법인명(상호)",normalFont));
		cell.setColspan(2);
		table.addCell(cell);
		cell = new Cell(new Paragraph("비즈머스",normalFont));
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		cell.setColspan(4);
		table.addCell(cell);
		cell = new Cell(new Paragraph("사업자등록번호",normalFont));
		cell.setColspan(2);
		table.addCell(cell);
		cell = new Cell(new Paragraph("제3423-342-342",normalFont));
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		table.addCell(cell);
		
		cell = new Cell(new Paragraph("대표자(성명)",normalFont));
		cell.setColspan(2);
		table.addCell(cell);
		cell = new Cell(new Paragraph("대표자",normalFont));
		cell.setColspan(4);
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		table.addCell(cell);
		cell = new Cell(new Paragraph("전  화 / 팩  스",normalFont));
		cell.setColspan(2);
		table.addCell(cell);
		cell = new Cell(new Chunk("042-123-1234",normalFont));
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		chunk = new Chunk("/",normalFont);
		cell.add(chunk);
		chunk = new Chunk("042-567-9876",normalFont);
		cell.add(chunk);
		table.addCell(cell);
		
		cell = new Cell(new Paragraph("주소(전자주소)",normalFont));
		cell.setColspan(2);
		table.addCell(cell);
		cell = new Cell(new Paragraph("대전광역시 서구 만년동대전광역시 서구 만년동대전광역시 서구 만년동 ",normalFont));
		cell.setColspan(7);
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		table.addCell(cell);
		
		/**공급물품명세서*/
		cell = new Cell(new Paragraph("공   급   물   품   명   세   서",boldFont));
		cell.setBackgroundColor(new Color(0x92,0xB6,0xBE));
		cell.setColspan(11);
		table.addCell(cell);
		
		cell = new Cell(new Chunk("3.연번",normalFont));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setRowspan(2);
		table.addCell(cell);
		cell = new Cell(new Chunk("4.품명-규격",normalFont));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setColspan(2);
		cell.setRowspan(2);
		table.addCell(cell);
		cell = new Cell(new Chunk("5.적용대상"+"\n"+"협정",normalFont));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setColspan(2);
		cell.setRowspan(2);
		table.addCell(cell);
		cell = new Cell(new Chunk("6.품목번호"+"\n"+"(HS 6단위)",normalFont));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setRowspan(2);
		table.addCell(cell);
		cell = new Cell(new Chunk("7.원산지"+"\n"+"결정기준",normalFont));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setRowspan(2);
		table.addCell(cell);
		cell = new Cell(new Chunk("8.원산지기준"+"\n"+"충족여부",normalFont));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setColspan(2);
		table.addCell(cell);
		cell = new Cell(new Chunk("9.원산지"+"\n"+"(국가)",normalFont));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setRowspan(2);
		table.addCell(cell);
		cell = new Cell(new Chunk("10.원산지포괄확인기간"+"\n"+"(년 월 일 ~ 년 월 일)",normalFont));
		cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
		cell.setRowspan(2);
		table.addCell(cell);
		cell = new Cell(new Chunk("충족",normalFont));
		table.addCell(cell);
		cell = new Cell(new Chunk("미충족",normalFont));
		table.addCell(cell);
		
		
		String [] checkIdx    = request.getParameterValues("idx");         // 체크박스 index
		String [] delivPartNo = request.getParameterValues("delivPartNo"); // 품명-규격
        String [] agreeCdNm   = request.getParameterValues("agreeCdNm");   // 협정명
        String [] hscd        = request.getParameterValues("hscd");        // 세번
        String [] origRule    = request.getParameterValues("origRule");    // 결정기준
        String [] satiYn      = request.getParameterValues("satiYN");      // 충족여부 (S:충족, F:불충족, B:)

        System.out.println("================>"+checkIdx.length);
        
		for(int i=0 ; i < checkIdx.length ; i++) {
		    
		    int idx = Integer.parseInt(checkIdx[i]);
		    
			cell = new Cell(new Chunk("1",normalFont));
			table.addCell(cell);
			cell = new Cell(new Chunk(delivPartNo[idx],normalFont));
			cell.setColspan(2);
			table.addCell(cell);
			cell = new Cell(new Chunk(agreeCdNm[idx],normalFont));
			cell.setColspan(2);
			table.addCell(cell);
			cell = new Cell(new Chunk(hscd[idx] ,normalFont));
			table.addCell(cell);
			cell = new Cell(new Chunk(origRule[idx] ,normalFont));
			table.addCell(cell);
			
			float x = 320; 
			float y = 475; 
			float x1 = 350; 
			
			cell = new Cell(new Chunk("충족",normalFont));
			cb = writer.getDirectContent();
			cb.setColorFill(new Color(255,255,255));
			cb.rectangle(x, y, 8, 8);
			cb.rectangle(x, y-(38*i), 8, 8);
			if (satiYn[i].equals("S")) {
			    cb.stroke();
            }
			table.addCell(cell);
			
			cell = new Cell(new Chunk("미충족",normalFont));
            cb.setColorFill(new Color(255,255,255));
            cb.rectangle(x1, y, 8, 8);
            cb.rectangle(x1, y-(38*i), 8, 8);
            if (satiYn[i].equals("F")) {
                cb.stroke();                  
            }			
			table.addCell(cell);
			
			cell = new Cell(new Chunk( (satiYn[i].equals("Y") ? "KR" : "") ,normalFont));
			table.addCell(cell);
			
			cell = new Cell(new Chunk("2010.08.20~2010.08.29",normalFont));
			table.addCell(cell);
		}

		cell = new Cell(new Paragraph("위 기재 내용은 사실과 다름이 없으며, 허위 또는 중대한 과실이 있는 때에는 그에 따른"+"\n"+"모든 법적 책임이 작성자에게 있음을 확인합니다.",normalFont));
		cell.setColspan(11);
		table.addCell(cell);
		
		cell = new Cell(new Paragraph("「자유무역협정의 이행을 위한 관세법의 특례에 관한 법률 시행규칙」 제6조의3제1항에 따라 작성-제공합니다.",normalFont));
		cell.setColspan(11);
		table.addCell(cell);
		
		Chunk bottom_ch;
		cell = new Cell("");
		cell.setBorderColor(new Color(255, 255, 255));
		cell.setColspan(4);
		table.addCell(cell);
		
		cell = new Cell(new Chunk("작     성     자 : ",normalFont));
		cell.setColspan(7);
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		bottom_ch = new Chunk("아 무 개 ",normalFont);
		cell.add(bottom_ch);
		bottom_ch = new Chunk("                                       (서명)"+"\n",normalFont);
		cell.add(bottom_ch);
		cell.setBorderColor(new Color(255, 255, 255));
		table.addCell(cell);
		
		cell = new Cell("");
		cell.setBorderColor(new Color(255, 255, 255));
		cell.setColspan(4);
		table.addCell(cell);
		cell = new Cell(new Chunk("직             위  : ",normalFont));
		cell.setColspan(7);
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		bottom_ch = new Chunk("사   원 ",normalFont);
		cell.add(bottom_ch);
		bottom_ch = new Chunk("\n",normalFont);
		cell.add(bottom_ch);
		cell.setBorderColor(new Color(255, 255, 255));
		table.addCell(cell);
		
		cell = new Cell("");
		cell.setBorderColor(new Color(255, 255, 255));
		cell.setColspan(4);
		table.addCell(cell);
		cell = new Cell(new Chunk("상 호 및 주 소 : ",normalFont));
		cell.setColspan(7);
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		bottom_ch = new Chunk("비 즈 머 스 ",normalFont);
		cell.add(bottom_ch);
		bottom_ch = new Chunk("\n",normalFont);
		cell.add(bottom_ch);
		cell.setBorderColor(new Color(255, 255, 255));
		table.addCell(cell);
		
		cell = new Cell("");
		cell.setBorderColor(new Color(255, 255, 255));
		cell.setColspan(4);
		table.addCell(cell);
		cell = new Cell(new Chunk("작  성  일  자 : ",normalFont));
		cell.setColspan(7);
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		bottom_ch = new Chunk("2010년 08월 19일 ",normalFont);
		cell.add(bottom_ch);
		bottom_ch = new Chunk("\n",normalFont);
		cell.add(bottom_ch);
		cell.setBorderColor(new Color(255, 255, 255));
		table.addCell(cell);
		
		document.add(table);
		
		Paragraph bottom = new Paragraph("210mm * 297mm(신문용지 54g/㎡(재활용품))",normalFont);
		bottom.setAlignment(Element.ALIGN_RIGHT);
		document.add(bottom);
	}
}